(function ($) {
	
	$.zjb_dialog = {};
	
	/**
	 * 打开对话框
	 * options		配置参数, 包含
	 * 		width		宽
	 * 		height		高
	 * 		title		标题
	 * 		content		内容
	 * 		buttons		按钮清单, 为数组, 每个元素包含
	 * 			name		按钮文本
	 * 			callback	回调函数
	 * 			isDefault	是否为默认按钮
	 * 		onLoad		对话框加载完毕回调函数, 形式
	 * 			function (contentDiv)
	 * 					其中 contentDiv 为装载 content 的父容器 
	 */
	function openDialog(options) {
		function bindClick(func, element, dialog) {
			element.click(function () {
				if (!!func && typeof func == 'function')
					func(dialog);
			});
		}
		
		var div = $('<div>');
		
		var html = [];
		html.push('<div class="zjbDlgMask"></div>');
		html.push('<div class="zjbDlgBody">');
		html.push('		<div class="zjbDlgTitle subsubtitle">' + options.title + '</div>');
		html.push('		<div class="zjbDlgContent"></div>');
		html.push('		<div class="zjbDlgFooter rightingText"></div>');
		html.push('</div>');
		
		$('body').append(div);
		div.addClass('zjbDlg');
		div.html(html.join('\r\n'));
		
		if (!!options.width) {
			div.find('.zjbDlgBody').css({
				width: options.width + 'px',
				'margin-left': ( -options.width / 2 - 4) + 'px'
			});
		}
		
		if (options.height <= 240)
			options.height = 240;
		
		div.find('.zjbDlgBody').css({
			height: options.height + 'px',
			'margin-top': ( -options.height / 2 - 16) + 'px'
		});
		
		div.find('.zjbDlgBody>.zjbDlgContent').css({
			height: (options.height - 76) + 'px'
		});
		
		var contentDiv = div.find('.zjbDlgBody>.zjbDlgContent');
		if (typeof options.content == 'string')
			contentDiv.html(options.content);
		else
			contentDiv.append(options.content);
		
		var buttons = options.buttons;
		var buttonsDiv = div.find('.zjbDlgFooter');
		
		for (var i=0; i<buttons.length; i++) {
			var button = buttons[i];
			var bDiv = $('<span class="zjbDlgButton button">' + button.name + '</span>');
			if (!!button.isDefault)
				bDiv.addClass('default');
			buttonsDiv.append(bDiv);
			bindClick(button.callback, bDiv, div);
		}
		
		if (!!options.onLoad && typeof options.onLoad == 'function')
			options.onLoad(contentDiv);
		
		return div;
	}
	
	/**
	 * 关闭对话框
	 * @param dialog
	 * @returns
	 */
	function closeDialog(dialog) {
		dialog.remove();
	}
	
	$.zjb_dialog.defaults = {
		width: null,
		height: null,
		title: '(无)',
		content: '(无)',
		buttons: [{
			name: '关闭', isDefault: true, callback: function (dialog) {
		    	closeDialog(dialog); 
		    }
		}],
		onLoad: function (contentDiv) {}
	};
	
	$.zjb_dialog.alert = function (title, content, callback, options) {
		return openDialog($.extend({
			title: title,
			content: content,
			buttons: [{
				name: '关闭', isDefault: true, callback: function (dialog) {
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback();
				}
			}]
		}, options || {}));
	};
	
	$.zjb_dialog.confirm = function (title, content, callback, options) {
		return openDialog($.extend({
			title: title,
			content: content,
			buttons: [{
				name: '确定', isDefault: true, callback: function (dialog) {
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback(true);
				}
			}, {
				name: '取消', callback: function (dialog) {
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback(false);
				}
			}]
		}, options || {}));
	};
	
	$.zjb_dialog.input = function (title, defaultValue, callback, options) {
		return openDialog($.extend({
			title: title,
			content: '<input type="text" class="centeringText" style="display: block; margin-top: 12px; width: 202px; line-height: 20px; font-size: 12px; padding: 5px; margin-left: auto; margin-right: auto;" />',
			buttons: [{
				name: '确定', isDefault: true, callback: function (dialog) {
					var text = dialog.find('input[type="text"]').val() || '';
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback(text);
				}
			}, {
				name: '取消', callback: function (dialog) {
					closeDialog(dialog);
				}
			}],
			onLoad: function (dialog) {
				dialog.find('input[type="text"]').val(defaultValue || '');
			}
		}, options || {}));
	};
	
	$.zjb_dialog.password = function (title, defaultValue, callback, options) {
		return openDialog($.extend({
			title: title,
			content: '<input type="password" class="centeringText" style="display: block; margin-top: 12px; width: 202px; line-height: 20px; font-size: 12px; padding: 5px; margin-left: auto; margin-right: auto;" />',
			buttons: [{
				name: '确定', isDefault: true, callback: function (dialog) {
					var password = dialog.find('input[type="password"]').val() || '';
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback(password);
				}
			}, {
				name: '取消', callback: function (dialog) {
					closeDialog(dialog);
				}
			}],
			onLoad: function (dialog) {
				dialog.find('input[type="password"]').val(defaultValue || '');
			}
		}, options || {}));
	};
	
	$.zjb_dialog.viewInfo = function (title, url, callback, options) {
		return openDialog($.extend({
			title: title,
			content: '<div class="zjbDialogInfo" style="overflow-y: auto;"></div>',
			width: 800,
			height: 640,
			buttons: [{
				name: '关闭', isDefault: true, callback: function (dialog) {
					closeDialog(dialog);
					if (!!callback && typeof callback == 'function')
						callback();
				}
			}],
			onLoad: function (dialog) {
				dialog.find('.zjbDialogInfo').load(url);
			}
		}, options || {}));
	};
	
	$.zjb_dialog.createDialog = function (options) {
		return openDialog(options);
	};
	
	$.zjb_dialog.removeDialog = function (dialog) {
		return closeDialog(dialog);
	};
	
})(jQuery);